function meta_analysis_characteristics(proj_meta,cur_sites)
fb_snip={};
pb_snip={};
fb_av={};
pb_av={};
vis_resp={};
vis_mean={};
run_resp={};
run_mean={};
blink_resp={};
blink_mean={};
eye_resp={};
eye_mean={};
ffm_resp={};
ffm_mean={};
blink_dark_resp={};
eye_dark_resp={};
vis_char=[];
run_char=[];
blink_char=[];
eye_char=[];
ffm_char=[];
fdbk_char=[];
blink_dark_char=[];
eye_dark_char=[];
fdbk_resp={};
fdbk_mean={};
ini_cnt=1;
ini_cnt_2=1;
for ond=1:length(cur_sites)
    cur_site=cur_sites(ond);
    order={};
    exp_inds={};
    for tnd=1:size(proj_meta(cur_site).rd,2)
        nbr_frames=proj_meta(cur_site).rd(1,tnd).nbr_frames;
        if length(nbr_frames)>5
            for xnd=1:length(nbr_frames)
                if nbr_frames(xnd)==5000
                    if xnd==length(nbr_frames)
                        order{tnd}(xnd)=3;
                    else
                        order{tnd}(xnd)=1;
                    end
                else
                    order{tnd}(xnd)=2;
                end
            end
            ord_ind=1:length(order{tnd});
            order{tnd}(max(ord_ind(order{tnd}==2))+1:end)=3;
        else
            order{tnd}=[1 2 1 2 3];
        end
        exp_inds{tnd}(1,1)=1;
        exp_inds{tnd}(1,2)=nbr_frames(1);
        for fnd=2:length(order{tnd})
            exp_inds{tnd}(fnd,1)=exp_inds{tnd}(fnd-1,2)+1;
            exp_inds{tnd}(fnd,2)=sum(nbr_frames(1:fnd));
        end
    end
    shad_all={};
    vis_fl_all={};
    pert_num={};
    for tnd=1:size(proj_meta(cur_site).rd,2)
        cur_pert_num=[];
        ps_id=proj_meta(cur_site).rd(1,tnd).ps_id;
        runM=proj_meta(cur_site).rd(1,tnd).velM_smoothed>0.005;
        runP=proj_meta(cur_site).rd(1,tnd).velP_smoothed>0.005;
        shad=[];
        vis_fl=[];
        cnt=1;
        for bnd=1:length(order{tnd})
            if order{tnd}(bnd)==1
                tmp_runM=runM(exp_inds{tnd}(bnd,1):exp_inds{tnd}(bnd,2));
                tmp_ps_id=ps_id(exp_inds{tnd}(bnd,1):exp_inds{tnd}(bnd,2));
                for fnd=1:length(tmp_runM)
                    if tmp_runM(fnd)==0
                        shad(cnt)=0;
                        vis_fl(cnt)=0;
                        cnt=cnt+1;
                    elseif tmp_runM(fnd)==1 & logical(tmp_ps_id(fnd))==0 %feedback
                        shad(cnt)=1;
                        vis_fl(cnt)=1;
                        cnt=cnt+1;
                    elseif tmp_runM(fnd)==1 & logical(tmp_ps_id(fnd))==1 %feedback mismatch
                        if tmp_ps_id(fnd)==7
                            shad(cnt)=3;
                            vis_fl(cnt)=0;
                            cnt=cnt+1;
                        else
                            cur_pert_num(cnt)=tmp_ps_id(fnd);
                            shad(cnt)=4;
                            vis_fl(cnt)=1;
                            cnt=cnt+1;
                        end
                    end
                end
            elseif order{tnd}(bnd)==2
                tmp_runM=runM(exp_inds{tnd}(bnd,1):exp_inds{tnd}(bnd,2));
                tmp_ps_id=ps_id(exp_inds{tnd}(bnd,1):exp_inds{tnd}(bnd,2));
                tmp_runP=runP(exp_inds{tnd}(bnd,1):exp_inds{tnd}(bnd,2));
                for fnd=1:length(tmp_runM)
                    if logical(tmp_ps_id(fnd))==0 & tmp_runM(fnd)==0 & tmp_runP(fnd)==0 | logical(tmp_ps_id(fnd))==1 & tmp_runM(fnd)==0 & tmp_runP(fnd)==0
                        shad(cnt)=0;
                        vis_fl(cnt)=0;
                        cnt=cnt+1;
                    elseif logical(tmp_ps_id(fnd))==1 & tmp_runM(fnd)==0 & tmp_runP(fnd)==1
                        if tmp_ps_id(fnd)==7
                            shad(cnt)=5;
                            vis_fl(cnt)=0;
                            cnt=cnt+1;
                        else
                            cur_pert_num(cnt)=tmp_ps_id(fnd);
                            shad(cnt)=6;
                            vis_fl(cnt)=1;
                            cnt=cnt+1;
                        end
                    elseif logical(tmp_ps_id(fnd))==0 & tmp_runM(fnd)==0 & tmp_runP(fnd)==1
                        shad(cnt)=2;
                        vis_fl(cnt)=1;
                        cnt=cnt+1;
                    elseif logical(tmp_ps_id(fnd))==0 & tmp_runM(fnd)==1 & tmp_runP(fnd)==1
                        shad(cnt)=1;
                        vis_fl(cnt)=1;
                        cnt=cnt+1;
                    elseif logical(tmp_ps_id(fnd))==0 & tmp_runM(fnd)==1 & tmp_runP(fnd)==0 | logical(tmp_ps_id(fnd))==1 & tmp_runM(fnd)==1 & tmp_runP(fnd)==0
                        shad(cnt)=3;
                        vis_fl(cnt)=0;
                        cnt=cnt+1;
                    elseif logical(tmp_ps_id(fnd))==1 & tmp_runM(fnd)==1 & tmp_runP(fnd)==1
                        if tmp_ps_id(fnd)==7
                            shad(cnt)=3;
                            vis_fl(cnt)=0;
                            cnt=cnt+1;
                        else
                            cur_pert_num(cnt)=tmp_ps_id(fnd);
                            shad(cnt)=4;
                            vis_fl(cnt)=1;
                            cnt=cnt+1;
                        end
                    end
                end
            end
        end
        shad_all{tnd}=shad;
        vis_fl(length(vis_fl):length(runM))=0;
        vis_fl_all{tnd}=vis_fl;
        pert_num{tnd}=cur_pert_num;
    end
    all_act={};
    for tnd=1:size(proj_meta(cur_site).rd,2)
        cnt=0;
        for vnd=1:size(proj_meta(cur_site).rd,1)
            all_act{tnd}(cnt+1:cnt+size(proj_meta(cur_site).rd(vnd,tnd).act,1),:)=proj_meta(cur_site).rd(vnd,tnd).act;
            cnt=cnt+size(proj_meta(cur_site).rd(vnd,tnd).act,1);
        end
    end
    
    for tnd=1:size(proj_meta(cur_site).rd,2)
        nbr_frames=proj_meta(cur_site).rd(1,tnd).nbr_frames;
        stim_id=proj_meta(cur_site).rd(1,tnd).ps_id;
        runP=proj_meta(cur_site).rd(1,tnd).velP_smoothed>0.005;
        runM=proj_meta(cur_site).rd(1,tnd).velM_smoothed>0.005;
        runM_raw=proj_meta(cur_site).rd(1,tnd).velM_smoothed;
        ini_cnt2=ini_cnt_2;
        for lnd=1:size(all_act{1},1)
            act=all_act{tnd}(lnd,:);
            [fb_av{ini_cnt2,tnd},pb_av{ini_cnt2,tnd},fb_snip{ini_cnt2,tnd},pb_snip{ini_cnt2,tnd}]=pert_ana_meta(nbr_frames,stim_id,runM,runP,runM_raw,act);
            ini_cnt2=ini_cnt2+1;
        end
    end
    ini_cnt_2=ini_cnt2;
    
    [vis_snips,run_snips,ffm_snips,fdbk_snips]=meta_analysis_find_onset_snips(proj_meta,cur_site,vis_fl_all,order,exp_inds,shad_all);
    [blink_snips,blink_snips_dark,eye_snips,eye_snips_dark]=meta_analysis_eye_onset(proj_meta,cur_site,order,exp_inds,shad_all);
    for ind=1:size(all_act{1},1)
        for tnd=1:size(proj_meta(cur_site).rd,2)
            cur_act=all_act{tnd}(ind,:);
            for hnd=1:size(vis_snips{tnd},1)
                vis_resp{ini_cnt,tnd}(hnd,:)=cur_act(vis_snips{tnd}(hnd,1):vis_snips{tnd}(hnd,2));
            end
            vis_mean{tnd}(ini_cnt,:)=mean(vis_resp{ini_cnt,tnd},1);
            vis_char(ini_cnt,tnd)=mean(vis_mean{tnd}(ini_cnt,11:20))-mean(vis_mean{tnd}(ini_cnt,1:10));
            for hnd=1:size(run_snips{tnd},1)
                run_resp{ini_cnt,tnd}(hnd,:)=cur_act(run_snips{tnd}(hnd,1):run_snips{tnd}(hnd,2));
            end
            run_mean{tnd}(ini_cnt,:)=mean(run_resp{ini_cnt,tnd},1);
            run_char(ini_cnt,tnd)=mean(run_mean{tnd}(ini_cnt,11:20))-mean(run_mean{tnd}(ini_cnt,1:10));
            for hnd=1:size(blink_snips{tnd},1)
                blink_resp{ini_cnt,tnd}(hnd,:)=cur_act(blink_snips{tnd}(hnd,1):blink_snips{tnd}(hnd,2));
            end
            blink_mean{tnd}(ini_cnt,:)=mean(blink_resp{ini_cnt,tnd},1);
            blink_char(ini_cnt,tnd)=mean(blink_mean{tnd}(ini_cnt,11:20))-mean(blink_mean{tnd}(ini_cnt,1:10));
            for hnd=1:size(eye_snips{tnd},1)
                eye_resp{ini_cnt,tnd}(hnd,:)=cur_act(eye_snips{tnd}(hnd,1):eye_snips{tnd}(hnd,2));
            end
            eye_mean{tnd}(ini_cnt,:)=mean(eye_resp{ini_cnt,tnd},1);
            eye_char(ini_cnt,tnd)=mean(eye_mean{tnd}(ini_cnt,11:20))-mean(eye_mean{tnd}(ini_cnt,1:10));
            for hnd=1:size(ffm_snips{tnd},1)
                ffm_resp{ini_cnt,tnd}(hnd,:)=cur_act(ffm_snips{tnd}(hnd,1):ffm_snips{tnd}(hnd,2));
            end
            ffm_mean{tnd}(ini_cnt,:)=mean(ffm_resp{ini_cnt,tnd},1);
            ffm_char(ini_cnt,tnd)=mean(ffm_mean{tnd}(ini_cnt,11:20))-mean(ffm_mean{tnd}(ini_cnt,1:10));
            for hnd=1:size(fdbk_snips{tnd},1)
                fdbk_resp{ini_cnt,tnd}(hnd,:)=cur_act(fdbk_snips{tnd}(hnd,1):fdbk_snips{tnd}(hnd,2));
            end
            fdbk_mean{tnd}(ini_cnt,:)=mean(fdbk_resp{ini_cnt,tnd},1);
            fdbk_char(ini_cnt,tnd)=mean(fdbk_mean{tnd}(ini_cnt,11:20))-mean(fdbk_mean{tnd}(ini_cnt,1:10));
            for hnd=1:size(blink_snips_dark{tnd},1)
                blink_dark_resp{ini_cnt,tnd}(hnd,:)=cur_act(blink_snips_dark{tnd}(hnd,1):blink_snips_dark{tnd}(hnd,2));
            end
            blink_dark_mean{tnd}(ini_cnt,:)=mean(blink_resp{ini_cnt,tnd},1);
            blink_dark_char(ini_cnt,tnd)=mean(blink_dark_mean{tnd}(ini_cnt,11:20))-mean(blink_dark_mean{tnd}(ini_cnt,1:10));
            for hnd=1:size(eye_snips_dark{tnd},1)
                eye_dark_resp{ini_cnt,tnd}(hnd,:)=cur_act(eye_snips_dark{tnd}(hnd,1):eye_snips_dark{tnd}(hnd,2));
            end
            eye_dark_mean{tnd}(ini_cnt,:)=mean(eye_resp{ini_cnt,tnd},1);
            eye_dark_char(ini_cnt,tnd)=mean(eye_dark_mean{tnd}(ini_cnt,11:20))-mean(eye_dark_mean{tnd}(ini_cnt,1:10));
        end
        ini_cnt=ini_cnt+1;
    end
end

[~,indi]=sort(max(vis_char(:,1:3),[],2));
figure;
scatter(1:976,vis_char(indi,1),35,[1 0 0],'fill')
hold on
scatter(1:976,vis_char(indi,2),35,[0 1 0],'fill')
scatter(1:976,vis_char(indi,3),35,[0 0 1],'fill')

[~,indi]=sort(max(run_char(:,1:3),[],2));
figure;
scatter(1:976,run_char(indi,1),35,[1 0 0],'fill')
hold on
scatter(1:976,run_char(indi,2),35,[0 1 0],'fill')
scatter(1:976,run_char(indi,3),35,[0 0 1],'fill')

[~,indi]=sort(max(fdbk_char(:,1:3),[],2));
figure;
scatter(1:976,fdbk_char(indi,1),35,[1 0 0],'fill')
hold on
scatter(1:976,fdbk_char(indi,2),35,[0 1 0],'fill')
scatter(1:976,fdbk_char(indi,3),35,[0 0 1],'fill')

fb_res=[];
fb_means=[];
pb_res=[];
pb_means=[];
for lnd=1:size(fb_av,1)
    for tnd=1:3
        for ynd=1:13
            cur_mean=fb_av{lnd,tnd}{ynd};
            if ~isempty(cur_mean)
                fb_res(lnd,tnd,ynd)=mean(cur_mean(11:20))-mean(cur_mean(1:10));
                fb_means(lnd,:,tnd,ynd)=cur_mean;
            else
                fb_res(lnd,tnd,ynd)=0;
                fb_means(lnd,:,tnd,ynd)=zeros(1,26);
            end
            cur_mean=pb_av{lnd,tnd}{ynd};
            if ~isempty(cur_mean)
                pb_res(lnd,tnd,ynd)=mean(cur_mean(11:20))-mean(cur_mean(1:10));
                pb_means(lnd,:,tnd,ynd)=cur_mean;
            else
                pb_res(lnd,tnd,ynd)=0;
                pb_means(lnd,:,tnd,ynd)=zeros(1,26);
            end
        end
    end
end

[~,indi]=sort(max(fb_res(:,1:3,13),[],2));
figure;
scatter(1:976,fb_res(indi,1,13),35,[1 0 0],'fill')
hold on
scatter(1:976,fb_res(indi,2,13),35,[0 1 0],'fill')
scatter(1:976,fb_res(indi,3,13),35,[0 0 1],'fill')

[~,indi]=sort(max(pb_res(:,1:3,13),[],2));
figure;
scatter(1:976,pb_res(indi,1,13),35,[1 0 0],'fill')
hold on
scatter(1:976,pb_res(indi,2,13),35,[0 1 0],'fill')
scatter(1:976,pb_res(indi,3,13),35,[0 0 1],'fill')

cell=12;
y_max=max([max(vis_resp{cell,1}(:)) max(vis_resp{cell,2}(:)) max(vis_resp{cell,3}(:))]);
figure;
subplot(1,3,1)
plot(vis_resp{cell,1}')
ylim([0.8 y_max])
subplot(1,3,2)
plot(vis_resp{cell,2}')
ylim([0.8 y_max])
subplot(1,3,3)
plot(vis_resp{cell,3}')
ylim([0.8 y_max])

figure;
cells=[80 12 212 13 155 223 203 743 428 427 330 296];
for hnd=1:length(cells)
    subplot(1,length(cells),hnd)
    hold on
    for tnd=1:3
        cur_trace=vis_mean{tnd}(cells(hnd),:);
        cur_trace=cur_trace-diff([1 cur_trace(11)]);
        plot(cur_trace,'color',[0 0 0]+((0.25)*(tnd-1)),'linewidth',2)
        ylim([0.8 2])
    end
end

figure;
cur_trace=mean(vis_mean{1},1);
cur_trace=cur_trace-diff([1 cur_trace(11)]);
plot(cur_trace,'r')
hold on
cur_trace=mean(vis_mean{2},1);
cur_trace=cur_trace-diff([1 cur_trace(11)]);
plot(cur_trace,'g')
cur_trace=mean(vis_mean{3},1);
cur_trace=cur_trace-diff([1 cur_trace(11)]);
plot(cur_trace,'b')

figure;
cur_trace=mean(fdbk_mean{1},1);
cur_trace=cur_trace-diff([1 cur_trace(11)]);
plot(cur_trace,'r')
hold on
cur_trace=mean(fdbk_mean{2},1);
cur_trace=cur_trace-diff([1 cur_trace(11)]);
plot(cur_trace,'g')
cur_trace=mean(fdbk_mean{3},1);
cur_trace=cur_trace-diff([1 cur_trace(11)]);
plot(cur_trace,'b')